package com.hi.hiparty.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.StringTokenizer;
import java.util.UUID;

public class FileUtils {
	public static final String SYS_SEPARATOR = File.separator;
	public static final String WINDOWS_PATH ="C:/images";
	public static final String LINUX_PATH = "/home/admin/images";
	
	static
	{

	}
	public static void init(){}
	
	public static void mkdir(String path) {
		StringTokenizer st = new StringTokenizer(path, "/");
		String path1 = st.nextToken() + "/";
		String path2 = path1;
		while (st.hasMoreTokens()) {
			path1 = st.nextToken() + "/";
			path2 += path1;
			File inbox = new File(path2);
			if (!inbox.exists())
				inbox.mkdir();
		}
	}
	
	public static boolean createFileIfExist(String path){
		try {
			return new File(path).createNewFile();
		} catch (IOException e) {
			return Boolean.FALSE;
		}

	}
	
	public static boolean isLinux() {
		if ("\\".equals(File.separator)) {
			return false;
		} 
		return true;
	}
	
	public static String getPath(){
		String path = "";
		if(isLinux()){
			path = FileUtils.LINUX_PATH;
		}else{
			path = FileUtils.WINDOWS_PATH;
		}
		mkdir(path);
		return path;
	}

	public static void writeStream2File(File file,InputStream in){
		try {
			BufferedOutputStream dos = new BufferedOutputStream(
					new FileOutputStream(file, false));
			BufferedInputStream dis = new BufferedInputStream(in);
			byte[] cbuf = new byte[1024];
			int count, i;
			while ((count = dis.read(cbuf)) != -1) {
				if (count == 1024)
					dos.write(cbuf);
				else {
					for (i = 0; i < count; i++) {
						dos.write(cbuf[i]);
					}
				}
			}
			dos.flush();
			dos.close();
			dis.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static String getUserAvatar(String userid){
		String[] tmp = new String[3];
		String avatarpath = "";
		try{
			Long i = Long.valueOf(userid);
			String ins = new Long(i + 100000000).toString();
			tmp[0] = ins.substring(ins.length() - 8, ins.length() - 6);
			tmp[1] = ins.substring(ins.length() - 6, ins.length() - 4);
			avatarpath = "/avatar/" + tmp[0] + "/" + tmp[1] + "/" + userid
					+ ".jpeg";
		}catch(Exception e){
			return avatarpath;
		}
		return avatarpath;
	}
	
	/**
	 * 
	 * @param userid
	 * @param fileSuffix
	 * @return
	 */
	public static String getUploadPicPath(String userid,String fileSuffix){
		String[] tmp = new String[3];
		//
		Long i = Long.valueOf(userid);
		String uploadPicPath = "";
		try{
			String ins = new Long(i + 100000000).toString();
			tmp[0] = ins.substring(ins.length() - 8, ins.length() - 6);
			tmp[1] = ins.substring(ins.length() - 6, ins.length() - 4);

			UUID uuid = UUID.randomUUID();
			String str = uuid.toString();
			// 去掉"-"符号
			tmp[2] = str.substring(0, 8) + str.substring(9, 13)
					+ str.substring(14, 18) + str.substring(19, 23)
					+ str.substring(24);
			uploadPicPath = "/upload/" + tmp[0] + "/" + tmp[1] + "/" + tmp[2]
					+ ((null == fileSuffix) ? ".jpeg" : "." + fileSuffix);
		}catch(Exception e){
			return uploadPicPath;
		}
		return uploadPicPath;
	}
	
	public static void main(String[] args){
		//mkdir("D:/test/test/test/test1/");
		System.out.println(getPath());
		System.out.println("ee".hashCode());
		System.out.println(getUserAvatar("12345678"));
	}
}
